package cn.com.surker.system.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.system.MenuEnum;
import cn.com.surker.system.dao.SysMenuInfoDao;
import cn.com.surker.system.entity.SysMenuInfo;
import cn.com.surker.system.service.MenuInfoService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author lucky
 * @create 2022年06月24 15:09
 * @desc
 */
@Service
public class MenuInfoServiceImpl implements MenuInfoService {

    @Resource
    private SysMenuInfoDao menuInfoDao;

    /**
     * 加载菜单
     * @return
     */
    public List<Map<String, Object>> loadMenu(Long type) {
        /**菜单结果集**/
        List<Map<String, Object>> list = new ArrayList<>();

        /**查询一级菜单信息**/
        List<Map<String, Object>> parenList = menuInfoDao.queryChildrenMenu(type,-1L);
        for (Integer i = 0; i < parenList.size(); i++) {
            Map<String, Object> map = parenList.get(i);
            /**获取父级ID**/
            Long pid = StringUtils.convertLong(map.get("id"));
            /**查询子菜单 **/
            List<Map<String, Object>> childrenList = menuInfoDao.queryChildrenMenu(type, pid);
            map.put("children", childrenList);
            list.add(map);
        }
        return list;
    }

    /**
     * 加载后台菜单
     * @param uid 用户ID
     * @param orgId 部门ID
     * @param type 类型,0:手机端菜单，1：后台管理菜单
     * @return
     */
//    @Cacheable(cacheNames = "userMenuInfo", key = "#uid")
    public List<Map<String, Object>> loadUserMenu(Long uid, Long orgId, Integer type) {
        /**菜单结果集**/
        List<Map<String, Object>> list = new ArrayList<>();

        /**查询一级菜单信息**/
        List<Map<String, Object>> parenList = menuInfoDao.queryOaUserChildrenMenu(uid, orgId, StateEnum.ENABLED.getCode(), -1L);
        for (Integer i = 0; i < parenList.size(); i++) {
            Map<String, Object> map = parenList.get(i);
            /**获取父级ID**/
            Long pid = StringUtils.convertLong(map.get("id"));
            /**查询子菜单 **/
            List<Map<String, Object>> childrenList = menuInfoDao.queryOaUserChildrenMenu(uid, orgId, StateEnum.ENABLED.getCode(), pid);
            map.put("children", childrenList);
            list.add(map);
        }

        return list;
    }
    /**
     * 加载后台菜单
     * @param uid 用户ID
     * @param loadType 是否加载旭科云菜单
     * @param type 类型,0:手机端菜单，1：后台管理菜单
     * @return
     */
//    @Cacheable(cacheNames = "userMenuInfo", key = "#uid")
    public List<Map<String, Object>> loadUserMenuV1(Long uid, Integer loadType, Integer type) {
        /**菜单结果集**/
        List<Map<String, Object>> list = new ArrayList<>();

        if (loadType == 1) {
            /**查询一级菜单信息**/
            List<Map<String, Object>> parenList = menuInfoDao.queryOaUserChildrenMenuV1(uid, null, StateEnum.ENABLED.getCode(), -1L);
            for (Integer i = 0; i < parenList.size(); i++) {
                Map<String, Object> map = parenList.get(i);
                /**获取父级ID**/
                Long pid = StringUtils.convertLong(map.get("id"));
                /**查询子菜单 **/
                List<Map<String, Object>> childrenList = menuInfoDao.queryErpUserChildrenMenuV2(uid, null, StateEnum.ENABLED.getCode(), MenuEnum.ERP_ADMIN.getCode(), pid);
                map.put("children", childrenList);
                list.add(map);
            }

            return list;
        } else {
            /**查询一级菜单信息**/
            List<Map<String, Object>> parenList = menuInfoDao.queryOaUserChildrenMenu(uid, null, StateEnum.ENABLED.getCode(), -1L);
            for (Integer i = 0; i < parenList.size(); i++) {
                Map<String, Object> map = parenList.get(i);
                /**获取父级ID**/
                Long pid = StringUtils.convertLong(map.get("id"));
                /**查询子菜单 **/
                List<Map<String, Object>> childrenList = menuInfoDao.queryErpUserChildrenMenuV2(uid, null, StateEnum.ENABLED.getCode(), MenuEnum.ERP_ADMIN.getCode(), pid);
                map.put("children", childrenList);
                list.add(map);
            }

            return list;
        }

    }

    /**
     * 禁用菜单
     * @param id
     * @return
     */
    public Integer delete(Long id) {
        if (StringUtils.isNull(id))
            return Code.NO_MENU.getCode();

        SysMenuInfo sysMenuInfo = new SysMenuInfo();
        sysMenuInfo.setId(id);
        sysMenuInfo.setStatus(StateEnum.DISABLED.getCode());

        Integer code = menuInfoDao.updateById(sysMenuInfo);
        if (code > 0)
            return 0;
        else
            return -1;
    }

    /**
     * 启用菜单
     * @param id
     * @return
     */
    public Integer enabled(Long id) {
        if (StringUtils.isNull(id))
            return Code.NO_MENU.getCode();

        SysMenuInfo sysMenuInfo = new SysMenuInfo();
        sysMenuInfo.setId(id);
        sysMenuInfo.setStatus(StateEnum.ENABLED.getCode());

        Integer code = menuInfoDao.updateById(sysMenuInfo);
        if (code > 0)
            return 0;
        else
            return -1;
    }

    /**
     * 分后台页查询数据
     * @param name 菜单名称
     * @param type 类型,0:手机端菜单，1：后台管理菜单
     * @param offset 起始位置
     * @param limit 页码大小
     * @return
     */
    public IPage findByPage(String name, Integer type, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<SysMenuInfo> query = new QueryWrapper();
        if (!StringUtils.isNull(type))
            query.eq("type", type);

        if (!StringUtils.isNull(name))
            query.like("name", name);

        IPage<SysMenuInfo> iPage = menuInfoDao.selectPage(new Page<>((offset + limit) / limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 新增数据
     * @param menu
     * @return
     */
    public Integer saveInit(SysMenuInfo menu) {
        if (menu != null)
            menu.setCreateTime(new Date());
        if (StringUtils.isNull(menu.getPid()))
            menu.setPid(1L);
        int saveStatus = menuInfoDao.insert(menu);
        if (saveStatus > 0) {
            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 更新数据
     * @param menu
     * @return
     */
    public Integer update(SysMenuInfo menu) {
        int saveStatus = menuInfoDao.updateById(menu);
        if (saveStatus > 0) {
            return 0;
        } else {
            return -1;
        }
    }
}
